package com.yc.io;

import java.io.File;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;

public class ZipCompressorByAnt {

    private File zipFile;   
      
    public ZipCompressorByAnt(String pathName) {   
        zipFile = new File(pathName);   
    }   
    
    /**
     * @param srcPathName 压缩目标
     */
    public void compress(String srcPathName) {
        File srcdir = new File(srcPathName);
        if (!srcdir.exists()) {
            throw new RuntimeException(srcPathName + "不存在！");   
        }
        Project prj = new Project();   
        Zip zip = new Zip();   
        zip.setProject(prj);   
        zip.setDestFile(zipFile);
        
        FileSet fileSet = new FileSet();   
        fileSet.setProject(prj);   
        fileSet.setDir(srcdir);   
        //fileSet.setIncludes("**/*.java"); 包括哪些文件或文件夹 eg:zip.setIncludes("*.java");   
        //fileSet.setExcludes(...); 排除哪些文件或文件夹   
        zip.addFileset(fileSet);   
        
        zip.execute();
    }
    
    public static void main(String[] args) {
    	ZipCompressorByAnt zb = new ZipCompressorByAnt("C:/Users/yuancan/.shr/temp/zip1563276160665.zip");
    	zb.compress("C:/Users/yuancan/.shr/temp/zip1563276160665");
    }

}